package com.ny.demo;

import java.io.*;

public class Demo21 {
    public static void main(String[] args) {
        String inputFilePath = "1.txt";
        String outputFilePath = "output.txt";

        // 使用 BufferedWriter 写入数据到文件
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(outputFilePath))) {
            // 写入一些示例数据
            writer.write("你快乐吗");
            writer.newLine();
            writer.write("我很快乐");
            System.out.println("数据已成功写入到 " + outputFilePath);
        } catch (IOException e) {
            System.err.println("写入文件时出错: " + e.getMessage());
        }

        // 使用 BufferedReader 从文件读取数据
        try (BufferedReader reader = new BufferedReader(new FileReader(inputFilePath))) {
            String line;
            System.out.println("从 " + inputFilePath + " 读取的数据如下:");
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            System.err.println("读取文件时出错: " + e.getMessage());
        }
    }

}
